package com.hehe.controller;

import com.hehe.entity.GoodsInfo;
import com.hehe.entity.GoodsInfo_Table;
import com.hehe.entity.GoodsType;
import com.hehe.entity.User;
import com.hehe.service.GoodsService;
import com.hehe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
//如果没有controller，jsp和class联系
@Controller
public class AdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "/UserManager")
    public ModelAndView UserManager(){

        ModelAndView mav = new ModelAndView("admin/ad_main");
        //用户管理
        List<User> users = userService.findAll();
        mav.addObject("user_lists",users);

        //商品管理
        List<GoodsInfo> goods = goodsService.findAll_goods();
        mav.addObject("goods_lists",goods);

        //类别管理
        List<GoodsType> types = goodsService.findAll_type();
        mav.addObject("type_lists",types);

        //首页商品选择
        List<GoodsInfo> show = goodsService.findShow_goods();
        mav.addObject("goods_show",show);

        return mav;
    }


    /**
     * 用户管理模块
     * @return
     */
    @RequestMapping (value = "ModifyUserStatus")
    private String ModifyUserStatus(@RequestParam("userid") int uid,@RequestParam("status") int status){

        int result = userService.updata_status(uid,status);
        if (result>0) System.out.println("修改成功");

        return "redirect:UserManager";
    }

    @RequestMapping (value = "DeleteUser")
    private String DeleteUser(@RequestParam int userid){
        int result = userService.del(userid);
        return "redirect:UserManager";
    }


    /**
     * 商品管理模块
     */

    @RequestMapping (value = "ModifyGoodsInfo")
    private String ModifyGoodsInfo(@RequestParam("gid") int gid,
                                   @RequestParam("name") String name,
                                    @RequestParam("price") String price,
                                   @RequestParam("show") int show){

        GoodsInfo_Table info =  new GoodsInfo_Table();
        info.setGoods_id(gid);
        info.setGoods_name(name);
        info.setGoods_price(price);
        info.setGoods_show_admin(show);
        int result = goodsService.update_goods_admin(info);
        if (result>0) System.out.println("修改成功");

        return "redirect:UserManager";
    }

    @RequestMapping (value = "DeleteGoods")
    private String DeleteGoods(@RequestParam int gid){
        int result = goodsService.del_goods(gid);
        return "redirect:UserManager";
    }

    /**
     * 显示模块
     */

    @RequestMapping (value = "DeleteShow")
    private String DeleteShow(@RequestParam int gid){
        int result = goodsService.update_show_admin(gid);
        return "redirect:UserManager";
    }

    /**
     * 商品类型管理
     */
    @RequestMapping (value = "Modify_type")
    private String Modify_type(int tid, String type_name){
        System.out.println(tid+type_name);
        GoodsType g = new GoodsType();
        g.setType_id(tid);
        g.setType_name(type_name);
        int result = goodsService.update_name(g);

        return "redirect:UserManager";
    }


}
